package com.nuaa.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.nuaa.model.hosp.Hospital;
import com.nuaa.vo.hosp.HospitalQueryVo;
import com.nuaa.yygh.cmn.client.DictFeignClient;
import com.nuaa.yygh.hosp.repository.HospitalRepository;
import com.nuaa.yygh.hosp.service.HospitalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    //注入数据库
    @Autowired
    private HospitalRepository hospitalRepository;

    //注入feign调用
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        //先将map转成字符串
        String jsonString = JSONObject.toJSONString(paramMap);
        //再将字符串转成Hospital对象
        Hospital hospital = JSONObject.parseObject(jsonString,Hospital.class);
        //判断是否存在,通过id判断
        Hospital targetHospital = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        //如果存在，这个id就行修改
        if(null != targetHospital) {
            hospital.setStatus(targetHospital.getStatus());
            hospital.setCreateTime(targetHospital.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {
            //没有就进行添加
            //0：未上线 1：已上线
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        //调用mongdb中repository的方法
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }


    //查的信息没有医院等级信息，调用cmn微服务获得
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //创建page对象
        Pageable page1 = PageRequest.of(page-1,limit);
        Hospital hospital = new Hospital();
        //将hospitalQueryVo对象转成hospital
        BeanUtils.copyProperties(hospitalQueryVo,hospital);

        //创建匹配器，就是怎么使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//改变默认字符匹配方式，模糊查询
                .withIgnoreCase(true); //忽略大小写

        Example<Hospital> example = Example.of(hospital,matcher);
        Page<Hospital> page2 = hospitalRepository.findAll(example,page1);

        //获取查询list集合，遍历进行医院等级封装
        //用java8新特性stream流进行遍历
        page2.getContent().stream().forEach(item->{
            //item是hospital对象
            this.setHospitalHosType(item);
        });

        return page2;
    }

    //更新医院状态
    @Override
    public void updateStatus(String id, Integer status) {
        if(status.intValue() == 0 || status.intValue() == 1){
            //通过mongodb的方法获取医院信息
            Hospital hospital = hospitalRepository.findById(id).get();
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    //获取医院详情
    @Override
    public Map<String, Object> show(String id) {
        Map<String,Object> res = new HashMap<>();
        //这样获得的医院详情里没有登记信息
        Hospital hospital1 = hospitalRepository.findById(id).get();
        //将其封装
        Hospital hospital = this.setHospitalHosType(hospital1);
        //放入map
        res.put("hospital",hospital);

        //处理预约信息
        res.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);

        return res;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(null != hospital){
            return hospital.getHosname();
        }
        return "";
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String,Object> result = new HashMap<>();
        //医院详情,将根据hoscode得到的医院详情进行等级封装
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        result.put("hospital",hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);

        return result;
    }


    //获取查询list集合，遍历进行医院等级封装
    private Hospital setHospitalHosType(Hospital hospital) {
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());

        hospital.getParam().put("hostypeString",hostypeString);
        
        //查省市区等
        String provinceSteing = dictFeignClient.getName(hospital.getProvinceCode());
        String citySteing = dictFeignClient.getName(hospital.getCityCode());
        String districtSteing = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("provinceSteing",provinceSteing);
        hospital.getParam().put("citySteing",citySteing);
        hospital.getParam().put("districtSteing",districtSteing);

        return hospital;
    }

}
